<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Request;
class Jurisdiction extends \app\index\controller\Common
{
	public function node_list()
	{
		$id=Request::instance()->get('id');
		$page=Request::instance()->get('page');
		$limit=Request::instance()->get('limit');
		//搜索条件
		$name=Request::instance()->get('name');
		$title=Request::instance()->get('title');
		//$type=Request::instance()->get('type');
		$map = array();
		$data = array();
		if ($name!="") {
			$map['name'] = ['like',"%$name%"];
		}
		if ($title!="") {
			$map['title'] = ['like',"%$title%"];
		}
		//$data=Db::table('auth_rule')->where($map)->page("$page,$limit")->select();
		$data=Db::table('auth_rule')->where($map)->select();
		//拥有权限的用户$data['lay_is_checked']=true;
		if ($id && $id!='undefined') {
			$auth_group=Db::table('auth_group')->where('id',$id)->find();
			$rules=explode(',', $auth_group['rules']);
			foreach ($data as $key => $value) {
				foreach ($rules as $k => $v) {
					if ($v==$value['id']) {
						$data[$key]['lay_is_checked']=true;
					}
				}
			}
		}

		$arr['code']='0';
		$arr['msg']='';
		$arr['count']=Db::table('auth_rule')->count();
		$arr['data']=$data;
		return $arr;
	}
	public function get_list()
	{
		$map = array();
		$map['status'] = '1';
		$data=Db::table('auth_rule')->where($map)->select();
		$this->result($data,0,'获取成功');
	}
	public function add()
	{
		$data = array();
		$data['pid']=Request::instance()->post('pid');
		$data['name']=rand(1,999).'/'.rand(1,999);//Request::instance()->post('name');
		$data['title']='新建权限';//Request::instance()->post('title');
		$data['type']='1';//Request::instance()->post('type');
		$data['condition']='';//Request::instance()->post('condition');
		if (Request::instance()->has('status','post')) {
			$data['status']=Request::instance()->post('status');
		}else{
			$data['status']='0';
		}
		Db::table('auth_rule')->insert($data);
		$id = Db::name('auth_rule')->getLastInsID();
		$data=Db::table('auth_rule')->where('id',$id)->find();
		$this->result($data,0,'提交成功');
	}
	public function edit()
	{
		
		if (Request::instance()->has('id','post')) {
			$id=Request::instance()->post('id');

			$data = array();
			$data['name']=Request::instance()->post('name');
			$data['title']=Request::instance()->post('title');
			$data['type']=Request::instance()->post('type');
			$data['condition']=Request::instance()->post('condition');
			if (Request::instance()->has('status','post')) {
				$data['status']=Request::instance()->post('status');
			}else{
				$data['status']='0';
			}
			Db::table('auth_rule')->where('id',$id)->update($data);
			$this->result($id,0,'提交成功');
		}
	}
	//三级以上不会删除
	public function del()
	{
		if (Request::instance()->has('id','post')) {
			$id=Request::instance()->post('id');
			if (Db::table('auth_rule')->delete($id)) {
				Db::table('auth_rule')->where('pid',$id)->delete();
				$this->result($id,0,'提交成功');
			}else{
				$this->result($id,1,'删除失败');
			}
		}
	}

}